/**
 * What you have to code if you want to convert a textarea to a richtext editor:
 * 0. page.addFeature("tinymce")
 * 1. Richtext.LoadConfiguration("someconfig"); 
 * 2. <textarea name="meetingDescTextArea" id="meetingDescTextArea" style="width:100%;"><%=meetingDescriptionText%></textarea>
 *		<script type="text/javascript" language="Javascript">
 *			Richtext.makeRich("meetingDescTextArea");
 *		</script>   
 * 3. Done :-)
 *
 */    

var Richtext = {
	LoadConfiguration: function(configName) {
	
		try {
			TinyMceConfigs[configName]();
		} catch (e) {
			alert("The configuration you does not exist. Loading Standard Config");
			TinyMceConfigs["VerySimpleTinyMce"]();
		}
	},
	
	makeRich: function(textarea) {
		var html = "<div class=\"buttongroup\">";
		html += "<input type=\"button\" style=\"width:80px\" value=\"Reset\" id=\"btnResetMessageDescriptionText\" name=\"btnResetMessageDescriptionText\" onclick=\"Richtext.reset('" + textarea + "')\" />";
		html += "</div>";

		new Insertion.After(textarea, html);
		
		tinyMCE.execCommand("mceAddControl", true, textarea);
	},
	
	reset: function(editorID) {
		var editor = $(editorID);
		tinyMCE.getInstanceById(editor.id).getBody().innerHTML = editor.defaultValue;
	}
};

/**
 * What you have to code to use EditableTinyMce:
 * 0. page.addFeature("tinymce") 
 * 1. Load the configuration once: EditableTinyMce.LoadConfiguration("configName");
 * 2. Insert as many editable fields as you want:
 *		<textarea class="editabletextarea" id="<%=dataName%>" name="<%=dataName%>"><%=initialValue%></textarea>
 *		<script type="text/javascript" language="Javascript">
 *				EditableTinyMce.InsertEditable("<%=dataName%>", {
 *				okMessage: "<%=OTGetUserMessage("Common", "OK")%>",
 *				cancelMessage: "<%=OTGetUserMessage("Common", "Cancel")%>",
 *				restoreMessage: "<%=OTGetUserMessage("Common", "Restore")%>",
 *				restoreConfirmationMessage: "<%=OTGetUserMessage("MeetingCenter", "RestoreTextConfirm")%>",
 *				editTextMessage: "<%=editTextVerbage%>"
 *			}); 
 * 3. Ready.
 */    

var EditableTinyMce = {

	tempTextArray: new Array(),
	saveShowField: false,
	
	LoadConfiguration: function(configName) {

		try {
			TinyMceConfigs[configName]();
		} catch (e) {
			alert("The configuration you did not");
			TinyMceConfigs["VerySimpleTinyMce"]();
		}

	},
	
	InsertEditable: function(dataName, i18nObj) {
	
		var textarea = $(dataName);
		
		// some assistance :-)
		if ((textarea == null) || (typeof(dataName) != 'string')) {
			alert("Place the Javascript code which calls InsertEditable() after the textarea with id \"" + dataName + "\"");
			return;
		}
		
		var initialValue = textarea.getValue();
		
		// this one needs the initial value
		var displayDiv = "<div id=\""+ dataName +"DisplayDiv\" name=\"" + dataName + "DisplayDiv\" class=\"editabletextareadisplay\" onclick=\"EditableTinyMce.EditText('" + dataName + "')\"></div>";

		// this one is the wrapper for the textarea 		
		var wrapperForTextarea = "<div class=\"editabletextarea\" id=\"" + dataName + "TextDiv\" name=\"" + dataName + "TextDiv\" style=\"display:none;\"></div>";
		
		var editLink = "<div id=\"" + dataName + "DisplayLinksDiv\" name=\"" + dataName + "DisplayLinksDiv\" class=\"small-black edittextarealink\"><a href=\"javascript: void EditableTinyMce.EditText('" + dataName + "')\">" + i18nObj['editTextMessage'] + "</a></div>"
		
		// links for ok, cancel, restore
		var controlLinks = "";
		controlLinks += "<div id=\"" + dataName + "EditLinksDiv\" name=\"" + dataName + "EditLinksDiv\" class=\"buttongroup\" style=\"display:none;\">";
		controlLinks += "<a id=\"" + dataName + "SaveLink\" href=\"javascript: void EditableTinyMce.SaveText('" + dataName + "', 1)\">" + i18nObj['okMessage'] + "</a>&nbsp;&nbsp;";
		controlLinks += "<a id=\"" + dataName + "CancelLink\" href=\"javascript: void EditableTinyMce.SaveText('" + dataName + "', 0)\">" + i18nObj['cancelMessage'] + "</a>&nbsp;&nbsp;";
		controlLinks += "<a id=\"" + dataName + "RestoreLink\" href=\"javascript: void EditableTinyMce.RestoreText('" + dataName + "', '" + i18nObj['restoreConfirmationMessage'] + "')\">" + i18nObj['restoreMessage'] + "</a>";
		controlLinks += "</div>";
		
		// insert the additional HTML we need
		new Insertion.Before(dataName, displayDiv);
		new Insertion.Before(dataName, wrapperForTextarea);
		new Insertion.After(dataName, controlLinks);
		new Insertion.After(dataName, editLink);
		
		// wrap the textarea with wrapperForTextarea
		$(dataName + "TextDiv").appendChild(textarea.remove());

		// displayDiv needs to show the initial value
		$(dataName + "DisplayDiv").innerHTML = initialValue;
	},
	
	EditText: function(txtPrefix) {
		$(txtPrefix + "TextDiv").show();
		$(txtPrefix + "EditLinksDiv").show();
		
		// cache text
		var currentEditor = $(txtPrefix);		
		this.tempTextArray[txtPrefix] = currentEditor.value;
		
		tinyMCE.execCommand("mceAddControl", true, txtPrefix);
		
		$(txtPrefix + "DisplayDiv").hide();
		$(txtPrefix + "DisplayLinksDiv").hide();
	},
	
	SaveText: function(txtPrefix, num) {
		var currentEditor = $(txtPrefix);
	
		if (num == 1) {
		
			var savedTextValue = tinyMCE.getInstanceById(txtPrefix).getBody().innerHTML;
		
			$(txtPrefix + "DisplayDiv").innerHTML = savedTextValue;

			if (this.saveShowField == true && $("SHOW_" + txtPrefix)) {			
				if (this.StripTags(savedTextValue) == '') {
					$("SHOW_" + txtPrefix).value = 'false';
				}
				else {
					$("SHOW_" + txtPrefix).value = '';				
				}
			}
			
		}	else {
			currentEditor.value = this.tempTextArray[txtPrefix];
		}
		
		if (currentEditor.value == '') {
			$(txtPrefix + "DisplayDiv").innerHTML = '';
		}

		
		$(txtPrefix + "TextDiv").hide();
		$(txtPrefix + "EditLinksDiv").hide();
		$(txtPrefix + "DisplayDiv").show();
		$(txtPrefix + "DisplayLinksDiv").show();
	},
	
	RestoreText: function(txtPrefix, msg) {
		if (confirm(msg)) {
			var mytextarea = $(txtPrefix);
			tinyMCE.getInstanceById(txtPrefix).getBody().innerHTML = mytextarea.defaultValue;
		}
	},
	
	StripTags: function(htmlText) {
	
		if (htmlText == null) {
			return null;
		}
		else {
			return htmlText.toString().replace(/<.*?>/g, '');
		}
		
	}
		
};
	
	// Configuration functions
	
var TinyMceConfigs = {
	
	language: window.OTLangID.toLowerCase().substr(0,2),
	
	StandardMeetingTinyMce: function() {
		tinyMCE.init({
			mode: "none",
			language: this.language, 
			theme: "advanced",
			plugins: "preview,table,fullscreen,paste",
			
			theme_advanced_buttons1: "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,formatselect,fontselect,fontsizeselect",
			theme_advanced_buttons2: "cut,copy,pastetext,pasteword,|,bullist,numlist,|,outdent,indent,|,undo,redo,|,link,unlink,anchor,image,cleanup,code,|,preview,|,forecolor,backcolor",
			theme_advanced_buttons3: "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,|,fullscreen",

			// preview stuff
			plugin_preview_width: "500",
			plugin_preview_height: "600",
	
			// paste stuff (as well word cleanup)
			paste_create_paragraphs: false,
			paste_create_linebreaks: false,
			paste_use_dialog: true,
			paste_auto_cleanup_on_paste: true, // MSIE only!
			paste_convert_middot_lists: true,
			//paste_unindented_list_class: "unindentedList",
			paste_convert_headers_to_strong: false,
			//paste_insert_word_content_callback: "convertWord"

			//onchange_callback : "EditableTinyMce.someFunction",
			
			content_css: "/javascript/tinymce/themes/advanced/skins/default/content.css", // as well needed for preview!
			theme_advanced_toolbar_location: "top",
			theme_advanced_toolbar_align: "left",
			theme_advanced_statusbar_location: "bottom",
			theme_advanced_resizing: true,
			use_native_selects: false
		});
	},
	
	TravelRulesAdmin: function() {
		tinyMCE.init({
			mode: "none",
			language: this.language, 
			theme: "advanced",
			plugins: "preview,table,fullscreen,paste",
			
			theme_advanced_buttons1: "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,formatselect,fontselect,fontsizeselect",
			theme_advanced_buttons2: "cut,copy,pastetext,pasteword,|,bullist,numlist,|,outdent,indent,|,undo,redo,|,link,unlink,anchor,image,cleanup,code,|,preview,|,forecolor,backcolor,|,fullscreen",
			theme_advanced_buttons3: "",

			// preview stuff
			plugin_preview_width: "500",
			plugin_preview_height: "600",
	
			// paste stuff (as well word cleanup)
			paste_create_paragraphs: false,
			paste_create_linebreaks: false,
			paste_use_dialog: true,
			paste_auto_cleanup_on_paste: true, // MSIE only!
			paste_convert_middot_lists: true,
			//paste_unindented_list_class: "unindentedList",
			paste_convert_headers_to_strong: false,
			//paste_insert_word_content_callback: "convertWord"

			//onchange_callback : "EditableTinyMce.someFunction",
			
			content_css: "/javascript/tinymce/themes/advanced/skins/default/content.css", // as well needed for preview!
			theme_advanced_toolbar_location: "top",
			theme_advanced_toolbar_align: "left",
			theme_advanced_statusbar_location: "bottom",
			theme_advanced_resizing: true,
			use_native_selects: false
		});
	},
	
	VerySimpleTinyMce: function() {
		alert("VerySimpleTinyMce()");
		
		tinyMCE.init({
			mode: "none",
			language: this.language,
			theme: "advanced",
			theme_advanced_toolbar_location : "top",
			theme_advanced_toolbar_align : "left",
			theme_advanced_statusbar_location : "bottom",
			theme_advanced_resizing : true,
			use_native_selects : false
		});
	}

};